
#ifdef _WIN32
#include <windows.h>
#include <gl\glut.h>
#elif __APPLE__
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#include "cGame.h"
#include "cAbstractGame.h"
#include "cMessageGame.h"

#define PLAYER_FILE "critter.png"

//Delete console
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

cAbstractGame* game[2];
int ngame=0;
int gameSt=0;
cData Data;

void AppRender()
{
	game[ngame]->Render();
}
void AppKeyboard(unsigned char key, int x, int y)
{
	game[ngame]->ReadKeyboard(key,x,y,true);
}
void AppKeyboardUp(unsigned char key, int x, int y)
{
	game[ngame]->ReadKeyboard(key,x,y,false);
}
void AppSpecialKeys(int key, int x, int y)
{
	game[ngame]->ReadKeyboard(key,x,y,true);
}
void AppSpecialKeysUp(int key, int x, int y)
{
	game[ngame]->ReadKeyboard(key,x,y,false);
}
void AppMouse(int button, int state, int x, int y)
{
	game[ngame]->ReadMouse(button,state,x,y);
}
void AppIdle(){
    int newgame;
    newgame=game[ngame]->Loop();
    if(newgame==EXIT_GAME_ST){
        for(int i=0; i<2; i++){
            delete game[i];
        }
        exit(0);
    }else if(newgame!=gameSt){
        if(newgame==CENTRAL_GAME_ST){
            game[CENTRAL_GAME]->Init(PECVJ2D_GAME_CFG, &Data, game[ngame]->GetCamera());
            game[CENTRAL_GAME]->SetEstate(CENTRAL_GAME_ST);
            ngame=CENTRAL_GAME;
        }else{
            game[MESSAGE_GAME]->SetEstate(newgame);
            ngame=MESSAGE_GAME;
        }
        gameSt=newgame;
    }
}

int main(int argc, char** argv){
    int res_x,res_y,pos_x,pos_y;

    //GLUT initialization
    glutInit(&argc, argv);

    //RGBA with double buffer
    glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE);

    //Create centered window
    res_x = glutGet(GLUT_SCREEN_WIDTH);
    res_y = glutGet(GLUT_SCREEN_HEIGHT);
    pos_x = (res_x>>1)-(CAMERA_WIDTH>>1);
    pos_y = (res_y>>1)-(CAMERA_HEIGHT>>1);
	
    glutInitWindowPosition(pos_x,pos_y);
    glutInitWindowSize(CAMERA_WIDTH,CAMERA_HEIGHT);
    glutCreateWindow("My first game!");

    /*glutGameModeString("800x600:32");
    glutEnterGameMode();*/

    //Make the default cursor disappear
    //glutSetCursor(GLUT_CURSOR_NONE);

    //Register callback functions
    glutDisplayFunc(AppRender);
    glutKeyboardFunc(AppKeyboard);
    glutKeyboardUpFunc(AppKeyboardUp);
    glutSpecialFunc(AppSpecialKeys);
    glutSpecialUpFunc(AppSpecialKeysUp);
    glutMouseFunc(AppMouse);
    glutIdleFunc(AppIdle);

    //Game initializations
    game[MESSAGE_GAME]=new cMessageGame();
    game[CENTRAL_GAME]=new cGame();
    
    game[MESSAGE_GAME]->Init(&Data, NULL);
    game[MESSAGE_GAME]->SetEstate(INTRO_GAME_ST);


    
    //Application loop
    glutMainLoop();

    return 0;
}
